package urban.view;

import model.User;
import controller.WebClientInterface;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UrbanViewActivitySenha extends Activity {
	String email;
	EditText senha;
	EditText novaSenha1;
	EditText novaSenha2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.senhalayout);
		
		Bundle b=this.getIntent().getExtras();
		email = b.getString("email");
		
		Button cadastrar = (Button) findViewById(R.id.buttonAlterar);
		senha = (EditText)findViewById(R.id.editText1);
		novaSenha1 = (EditText)findViewById(R.id.editText2);
		novaSenha2 = (EditText)findViewById(R.id.editText3);
		cadastrar.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bundle b = new Bundle();
				b.putString("email", email);
				Intent in = new Intent(UrbanViewActivitySenha.this, UrbanViewActivityLogado.class);
				in.putExtras(b);
				
				
				
				
				User user = WebClientInterface.readUser(email);
				if(!novaSenha1.getText().toString().equals("") && !novaSenha2.getText().toString().equals(""))
				{
					if(novaSenha1.getText().toString().equals(novaSenha2.getText().toString()))
					{
						if(user.getPassword().equals(senha.getText().toString()))
						{
							user.setPassword(novaSenha1.getText().toString());
							WebClientInterface.updateUser(user);
							startActivityForResult(in, 1);
							makeText("Senha alterada!");
						}
						else
						{
							makeText("Senha atual errada!");
						}
					}
					else
					{
						makeText("Senhas nao batem!");
					}
				}
				else
				{
					makeText("Digite uma senha!");
				}
				
			}
			
		});
	}
	
	private void makeText(String str)
	{
		Toast.makeText(this, str, Toast.LENGTH_LONG).show();
	}

}
